// src/services/sponsorDashboardService/sponsorContactsBySponsorId.ts

import { BASE_URL } from '@/services/apiConfig';

export interface SponsorContactItem {
  sponsor_contact_id: number;
  sponsor_id: number;
  title?: string;
  first_name?: string;
  last_name?: string;
  position_title?: string;
  phone?: string;
  ec?: string;
  gm?: string;
  kc?: string;
  ss?: string;
  lk?: string;
  inv?: string;
  aa?: string;
  bb?: string;
  mobile?: string;
  fax?: string;
  email?: string;
  website?: string;
  last_updated?: string | null; // 修复：可以是字符串或 null
  archive?: string;
  hidden?: string;
}

export interface FetchSponsorContactsParams {
  sponsorId: number;
  kc?: boolean;
  inv?: boolean;
}

export async function fetchSponsorContactsBySponsorId(
  params: FetchSponsorContactsParams
): Promise<SponsorContactItem[]> {
  try {
    const { sponsorId, kc, inv } = params;
    const queryParams = new URLSearchParams();
    if (kc !== undefined) {
      queryParams.append('kc', String(kc));
    }
    if (inv !== undefined) {
      queryParams.append('inv', String(inv));
    }
    const url = `${BASE_URL}/sponsors/${sponsorId}/contacts?${queryParams.toString()}`;
    const response = await fetch(url, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    });
    if (!response.ok) {
      throw new Error(`Failed to fetch sponsor contacts: ${response.status} ${response.statusText}`);
    }
    const data: SponsorContactItem[] = await response.json();
    return data;
  } catch (error) {
    console.error('Error fetching sponsor contacts:', error);
    throw error;
  }
}

export async function getSponsorContacts(sponsorId: number): Promise<SponsorContactItem[]> {
  return fetchSponsorContactsBySponsorId({ sponsorId });
}